//
//  RanktablevieCell.m
//  CoolTennisBall
//
//  Created by Coollang on 2017/7/27.
//  Copyright © 2017年 CoolLang. All rights reserved.
//

#import "RanktablevieCell.h"
#import "MyRankInfoModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIImageView+CM_ShowImage.h"

NSString * const RanktablevieCellIdentifierID = @"RanktablevieCell";

@interface RanktablevieCell ()

@property (weak, nonatomic) IBOutlet UILabel *rankingLabel;
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UIImageView *rankLocationImg;
@property (weak, nonatomic) IBOutlet UILabel *likesCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *kilometerLabel;
@property (weak, nonatomic) IBOutlet UILabel *unitLabel;
@property (weak, nonatomic) IBOutlet UIView *levelView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomContraitLevel;
@property (weak, nonatomic) IBOutlet UIButton *likeButton;

@end

@implementation RanktablevieCell

- (IBAction)likeButtonClick:(UIButton *)sender {
    if (self.cellLikeHandelBlock){
        self.cellLikeHandelBlock(^(BOOL success) {
            if (success){
                sender.selected = !sender.isSelected;
                NSInteger change = sender.isSelected ? 1:-1;
                
                NSInteger likesCount = self.likesCountLabel.text.integerValue + change;
                self.likesCountLabel.hidden = likesCount <= 0;
                self.likesCountLabel.text = @(likesCount).stringValue;
            }
        });
    }
}

- (void)configureCompetitionCellByModel:(RankingModel *)model indexPath:(NSIndexPath *)indexPath rankModeType:(RankModeType)type {
    switch (type) {
        case RankModeTypeSwings:
            self.unitLabel.text = NSLocalizedString(@"Sport.Unit.Shots", nil);
            self.kilometerLabel.text = model.totalCount;
            break;
        case RankModeTypeMaxSpeed:
            self.unitLabel.text = @"km/h";
            self.kilometerLabel.text = model.maxSpeed;
            break;
        case RankModeTypeAvgScore:
            self.unitLabel.text = NSLocalizedString(@"Unit.Score", nil);
            self.kilometerLabel.text = model.score;
            break;
    }
    
    self.rankingLabel.text = [NSString stringWithFormat:@"%ld",(long)(indexPath.row + 4)];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:model.headIcon] placeholderImage:[UIImage imageNamed:@"competition_icon_defaulImage"]];
    
    self.nameLabel.text = model.userName;
    
    self.rankLocationImg.hidden = (model.address.length <= 0);
    self.addressLabel.text = model.address;
    self.likesCountLabel.text = model.likes;
    self.likesCountLabel.hidden = model.likes.integerValue <= 0;
    self.likeButton.selected = model.isLiked;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showHeadIconImage)];
    self.headImage.userInteractionEnabled = YES;
    [self.headImage addGestureRecognizer:tap];
}

- (void)showHeadIconImage {
    [self.headImage showImage];
}

@end
